package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Grade;
import com.sky.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 尹稳健~
 * @description: 控制器-管理年级信息页面
 * @version 1.0
 */
@Controller
@RequestMapping("/grade")
public class GradeController {

    // 注入业务对象
    @Autowired
    @Qualifier("gradeServiceImpl")
    private GradeService gradeService;

    // 存储预返回页面的结果对象
    private Map<String,Object> result = new HashMap<String, Object>();

    /**
     * @description: 跳转到年级信息管理页面
     * @param: no
     * @return: java.lang.String
     */
    @RequestMapping("/goGradeList")
    public String goGradeList(){
        return "/grade/gradeList";
    }

    /**
     * @description: 分页查询:根据年级名称获取指定/所有年级信息列表
     * @param: page
     * @param: rows
     * @param: gradename
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/getGradeList")
    @ResponseBody
    public Map<String,Object> getGradeList(Integer page,Integer rows,String gradeName){
        // 设置每页的记录数
        PageHelper.startPage(page,rows);
        // 创建年级信息对象进行封装
        Grade grade = new Grade(gradeName);
        // 根据年级名称获取指定或全部年级信息列表
        List<Grade> gradeList = gradeService.getGradeList(grade);
        //封装信息列表
        PageInfo<Grade> gradePageInfo = new PageInfo<Grade>(gradeList);
        // 获取当前页数据列表
        List<Grade> list = gradePageInfo.getList();
        // 获取总记录数
        long total = gradePageInfo.getTotal();
        // 存储数据对象
        result.put("total",total);
        result.put("rows",list);
        System.out.println(result);
        return result;
    }

    /**
     * @description: 添加年级信息
     * @param: grade
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/addGrade")
    @ResponseBody
    public Map<String,Object> addGrade(Grade grade){
        //判断年级名是否已存在
        Grade g = gradeService.findByName(grade.getName());
        if (g!=null){
            result.put("success",false);
            result.put("msg","该年级名称已存在! 请修改后重试!");
            return result;
        }
        // 添加操作
        gradeService.addGrade(grade);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id修改指定的年级信息
     * @param: grade
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/editGrade")
    @ResponseBody
    public Map<String,Object> editGrade(Grade grade){
        //需排除用户只修改年级名以外的信息
        Grade g = gradeService.findByName(grade.getName());
        if (g!=null){
            if (!g.getId().equals(grade.getId())){
                result.put("success",false);
                result.put("msg","该年级名称已存在! 请修改后重试!");
                return result;
            }
        }
        // 修改操作
        gradeService.editGrade(grade);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id删除指定的年级信息
     * @param: ids 拼接后的id
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/deleteGrade")
    @ResponseBody
    public Map<String,Object> deleteGrade(@RequestParam(value = "ids[]",required = true) Integer[] ids){
        gradeService.deleteGrade(ids);
        result.put("success",true);
        return result;
    }
}
